'''xpath_案例2爬取站长素材
爬取站长素材网址：https://sc.chinaz.com/tupian/

分页规则示例：
        https://sc.chinaz.com/tupian/
        https://sc.chinaz.com/tupian/index_2.html
        https://sc.chinaz.com/tupian/index_3.html
        ......
'''
# 引入操作文件包
import os
# 引入操作网络请求包
import urllib.request
# 引入解析html包
from lxml import etree


# 定义请求Request
def create_request(page):
    if page == 1:
        zzsc_url = 'https://sc.chinaz.com/tupian/'
    else:
        zzsc_url = 'https://sc.chinaz.com/tupian/index_{0}.html'.format(page)
    print(zzsc_url) # https://sc.chinaz.com/tupian/index_2.html  https://sc.chinaz.com/tupian/index_3.html

    zzsc_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url=zzsc_url, headers=zzsc_headers)
    return request


# 获取请求返回体
def get_response(request):
    response = urllib.request.urlopen(request)
    html = response.read().decode('utf-8')
    # print(html)
    return html

# 下载图片
def down_img(page, name_list, img_list):
    # 注意：一定要保证 D:\\hxy\\test\\爬取站长素材 文件夹存在！
    save_local_path = 'D:\\hxy\\test\\爬取站长素材\\'+str(page)
    exit_falg = os.path.exists(save_local_path)
    if exit_falg == False:
        # 文件加不存在, 创建文件夹
        os.mkdir(save_local_path)
    # 循环list 容器下载图片
    for index in range(len(name_list)):
        print(name_list[index], img_list[index])
        down_img_url = 'https:'+img_list[index]
        down_img_local = save_local_path+'\\'+name_list[index]+'.jpg'
        print(down_img_url, down_img_local)
        urllib.request.urlretrieve(down_img_url, down_img_local)



if __name__ == '__main__':
    start_num = int(input('请输入起始页：'))
    # 三元表达式 条件一 else 条件二
    start_num = 1 if start_num <= 0 else start_num
    end_num = int(input('请输入起始页：'))
    for page in range(start_num, end_num+1):
        request = create_request(page)
        html = get_response(request)
        # 解析html
        html_parse = etree.HTML(html)
        # xpath获取 图片名称
        name_list = html_parse.xpath('//div[@class="container"]//img/@alt')
        # xpath获取 图片路径
        img_list = html_parse.xpath('//div[@class="container"]//img/@data-original')
        # print(len(name_list), name_list)
        # print(len(img_list), img_list)
        down_img(page, name_list, img_list)



    '''
    #初始示例
    request = create_request(1)
    html = get_response(request)

    html_parse = etree.HTML(html)
    name_list = html_parse.xpath('//div[@class="container"]//img/@alt')
    img_list = html_parse.xpath('//div[@class="container"]//img/@data-original')
    print(len(name_list), name_list)
    print(len(img_list), img_list)

    for index in range(len(name_list)):
        print(name_list[index], img_list[index])
        down_img_url = 'https:'+img_list[index]
        exitFlag = os.mkdir('D:\\hxy\\test\\爬取站长素材')
        print(exitFlag)
        # down_img_local = 'D:\\hxy\\test\\爬取站长素材\\{0}.jpg'.format(name_list[index])
        # print(down_img_url, down_img_local)
        # urllib.request.urlretrieve(down_img_url, down_img_local)
    '''


